package sfsoftware.game.trevor.ui;

import java.awt.Font;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.prefs.Preferences;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;

import sfsoftware.game.TrevorLegacy;
import sfsoftware.util.Clock;
import sfsoftware.util.Sound;
import sfsoftware.util.StringUtils;
import sfsoftware.util.graphics.Graphics;
import sfsoftware.util.graphics.RGBA;
import sfsoftware.util.input.Gamepad;
import sfsoftware.util.input.Kboard;

public class MainMenu {
	private static Properties properties = new Properties();
	private static boolean fade = false;
	private static float fadeLevel = 0;
	private static float fadeLevel2 = 0.95f;
	
	private static Clock soundTestclock = null;
	
	private static enum titleMenuStates 
	{
		PRESS_SPACE, MAIN, PLAY_GAME, CONTINUE_GAME, SETTINGS, ABOUT, EXIT;
	}
	
	private static titleMenuStates TitleMenuState = titleMenuStates.PRESS_SPACE;
	private static final TrueTypeFont MENU_FONT;
	private static final TrueTypeFont TINY_FONT;
	private static final String[] MAIN_MENU_OPTIONS = new String[] {"New Game","Continue Game","Settings", "About", "Exit"};
	private static final String[] SETTINGS_OPTIONS = new String[] {"Full Screen","V-Sync","Sound Volume","Music Volume","Return to Main Menu"};
	private static final String[] CONTINUE_OPTIONS = new String[] {"File A","File B","File C","File D","Return to Main Menu"};
	private static String[] currentMenu = MAIN_MENU_OPTIONS;
	private static int titleSelection = 0;	
	private static int titleHeight = 0;
	private static int titleBgX = 0;
	private static int titleBgY = 0;
	private static float menuTextFadeLevel = 0;
	
	static 
	{
		Font awtFont = new Font("Verdana", Font.PLAIN, 20);
		MENU_FONT = new TrueTypeFont(awtFont, true);	
		
		awtFont = new Font("Verdana", Font.PLAIN, 9);
		TINY_FONT = new TrueTypeFont(awtFont, true);
		
		try 
		{
            //load a properties file
			properties.load(new FileInputStream("res/game.properties"));			
		} 
		catch (IOException ex) 
		{			
			ex.printStackTrace();
		}	
	}
	
	public MainMenu() {
		// TODO Auto-generated constructor stub
	}

	private static String booleanToPrettyText(boolean b)
	{
		if(b)
		{
			return "Enabled";
		}
		else
		{
			return "Disabled";
		}
	}
	
	private static void drawBackground()
	{
		for(int i=-256; i <= 640; i = i + 256)
		{
			for(int j=-256; j <= 608; j = j + 256)
			{
				Graphics.drawSprite(i + titleBgX, j + titleBgY, "title_bg", 256, 256, 0, 0, 256, 256, 0.75f, 0.75f, 1, 1);
			}
		}
		
		RGBA top = new RGBA(0, 0, 0, fadeLevel);//0.35f);
		RGBA bottom = new RGBA(0, 0, 0, 0.95f);	
		
		if(fade)
		{
			fadeLevel = fadeLevel - 0.001f;
			fadeLevel2 = fadeLevel2 + 0.001f;
			if(fadeLevel <= 0)
			{
				fadeLevel = 0;
				fade = false;
			}
		}
		else
		{
			fadeLevel = fadeLevel + 0.001f;
			fadeLevel2 = fadeLevel2 - 0.001f;
			if(fadeLevel >= 0.65f)
			{
				fadeLevel = 0.65f;
				fade = true;
			}
		}
		
		Graphics.drawQuad(0, 0, 640, 480, top, bottom, bottom, top);				
		//Graphics.drawCircle(320, 240, 500, 0, 0, 0, 0, 1, 1, 1, 0.85f);
		
		if(titleBgX <= 256)
		{
			titleBgX++;
			titleBgY--;
		}
		else
		{
			titleBgX = 0;
			titleBgY = 0;
		}
	}
	
	public static void pollInput()
	{		
		if(titleHeight >= 128)
		{
		if (Kboard.keyPressed(Keyboard.KEY_ESCAPE) || Gamepad.buttonPressed(Gamepad.BUTTON_B)) 
		{		    
	    	switch(TitleMenuState)
	    	{
	    		case PRESS_SPACE:	 
	    			TrevorLegacy.setRunning(false);		    			
	    			break;
	    		case SETTINGS:
	    			titleSelection = 0;
	    			TitleMenuState = titleMenuStates.MAIN;
	    			currentMenu = MAIN_MENU_OPTIONS;
	    			break;
	    		default:
	    			TitleMenuState = titleMenuStates.PRESS_SPACE;
	    			break;
	    	}
		} 
		

		if(Kboard.keyPressed(Kboard.leftKeys) || Gamepad.povXLeftPressed() || Gamepad.xAxisLeftPressed())
	    {	
			if(TitleMenuState == titleMenuStates.SETTINGS)
			{
				if(titleSelection == 2)
				{
					// Sound volume
					Sound.setSoundEffectVolume(Sound.getSoundEffectVolume() - 0.10f);
					if(Sound.getSoundEffectVolume() < 0)
					{
						Sound.setSoundEffectVolume(0);
					}
					Sound.playSound("menu_go");
				}
				else if(titleSelection == 3)
				{
					// Music volume
					Sound.setMusicVolume(Sound.getMusicVolume() - 0.10f);
					if(Sound.getMusicVolume() < 0)
					{
						Sound.setMusicVolume(0);
					}
					Sound.playSound("menu_go");
				}
			}
	    }

		if(Kboard.keyPressed(Kboard.rightKeys) || Gamepad.povXRightPressed() || Gamepad.xAxisRightPressed())
	    {	
			if(TitleMenuState == titleMenuStates.SETTINGS)
			{
				if(titleSelection == 2)
				{
					// Sound volume
					Sound.setSoundEffectVolume(Sound.getSoundEffectVolume() + 0.10f);
					if(Sound.getSoundEffectVolume() > 1)
					{
						Sound.setSoundEffectVolume(1);
					}
					Sound.playSound("menu_go");
				}
				else if(titleSelection == 3)
				{
					// Music volume
					Sound.setMusicVolume(Sound.getMusicVolume() + 0.10f);
					if(Sound.getMusicVolume() > 1)
					{
						Sound.setMusicVolume(1);
					}
					Sound.playSound("menu_go");
				}
			}
	    }		
		
		// The player has made a selection.
		if (Kboard.keyPressed(Keyboard.KEY_SPACE) || Gamepad.buttonPressed(Gamepad.BUTTON_A)) {		   
	    	Sound.playSound("menu_go");
	    	switch(TitleMenuState)
	    	{
	    		case PRESS_SPACE:
	    			TitleMenuState = titleMenuStates.MAIN;
	    			break;
	    		case MAIN:
	    			switch(titleSelection)
	    			{
	    				case 0:
	    					// New game	    					
	    					TrevorLegacy.newGame();
	    					break;
	    				case 1:
	    					// Continue game
	    					titleSelection = 0;
	    					TitleMenuState = titleMenuStates.CONTINUE_GAME;
	    					currentMenu = CONTINUE_OPTIONS;
	    					break;
	    				case 2:
	    					// Settings
	    					titleSelection = 0;
	    					TitleMenuState = titleMenuStates.SETTINGS;
	    					currentMenu = SETTINGS_OPTIONS;
	    					soundTestclock = new Clock(750);
	    					break;
	    				case 3:
	    					// About
	    					titleSelection = 0;
	    					TitleMenuState = titleMenuStates.ABOUT;
	    					break;		    							    					
	    				case 4:
	    					// Exit
			    			TrevorLegacy.setRunning(false);		    			
			    			break;
	    			}
	    			break;
	    		case CONTINUE_GAME:
	    			switch(titleSelection)
	    			{
	    				// TODO Handle Loading games here!
	    				default:
	    					titleSelection = 0;
	    					TitleMenuState = titleMenuStates.MAIN;
	    					currentMenu = MAIN_MENU_OPTIONS;	
	    					break;
	    			}
	    			break;
	    		case ABOUT:
					titleSelection = 0;
					TitleMenuState = titleMenuStates.MAIN;
					currentMenu = MAIN_MENU_OPTIONS;	
					break;
	    		case SETTINGS:
	    			switch(titleSelection)
	    			{
	    				case 0:
	    					TrevorLegacy.setDisplayMode(640, 480, !Display.isFullscreen());
	    					Mouse.setGrabbed(Display.isFullscreen());
	    					break;
	    				case 1:
	    	        		TrevorLegacy.vsync = !(TrevorLegacy.vsync);	        
	    	        		Display.setVSyncEnabled(TrevorLegacy.vsync);
	    					break;
	    				case 2:
	    					// Sound volume	    					
	    					break;
	    				case 3:
	    					// Music volume
	    					break;
	    				default:
	    					savePreferences();
	    					titleSelection = 0;
	    					TitleMenuState = titleMenuStates.MAIN;
	    					currentMenu = MAIN_MENU_OPTIONS;
	    					break;
	    			}
	    		default:
	    			break;
	    	}	    
		} 
		if(TitleMenuState != titleMenuStates.PRESS_SPACE && TitleMenuState != titleMenuStates.ABOUT)
		{
			if (Kboard.keyPressed(Kboard.downKeys) || Gamepad.povYDownPressed()  || Gamepad.yAxisDownPressed()) 
			{        		
				Sound.playSound("menu_sel");
				titleSelection++;
				if(titleSelection >= currentMenu.length)
				{
					titleSelection = 0;
				}
			}
			else if (Kboard.keyPressed(Kboard.upKeys) || Gamepad.povYUpPressed()  || Gamepad.yAxisUpPressed())
			{
				Sound.playSound("menu_sel");
				titleSelection--;
				if(titleSelection < 0)
				{
					titleSelection = currentMenu.length - 1;
				}				
			}
		}
		}
	}
	
	private static void savePreferences()
	{
		Preferences prefs = Preferences.userNodeForPackage(sfsoftware.game.TrevorLegacy.class);
		
		prefs.putBoolean("fullscreen", Display.isFullscreen());
		prefs.putBoolean("vsync", TrevorLegacy.vsync);
		prefs.putFloat("soundvolume", Sound.getSoundEffectVolume());
		prefs.putFloat("musicvolume", Sound.getMusicVolume());
	}
	
	public static void update()
	{
		int TitleY = 175;		
		
		// Draw the background stuff
		drawBackground();	
		
		// If the title text isn't full expanded yet, increase the height.
		if(titleHeight < 128)
		{
			titleHeight = titleHeight + 2;
		}
		else
		{
			titleHeight = 128;
			
			if(menuTextFadeLevel < 1)
			{
				menuTextFadeLevel = menuTextFadeLevel + 0.025f;
			}
			else
			{
				menuTextFadeLevel = 1;
			}
			Color fadeColor = new Color(1, 1, 1, menuTextFadeLevel);
			
			// Draw the copyright text - very important so people don't go ripping us off ;)
			String copyrightText = StringUtils.COPYRIGHT_SYMBOL + " S&F SOFTWARE 2014";
			TINY_FONT.drawString(640 - (TINY_FONT.getWidth(copyrightText) + 10), 480 - (TINY_FONT.getHeight() + 10), copyrightText, fadeColor);
			
			switch(TitleMenuState)
			{			
				case PRESS_SPACE:			
					MENU_FONT.drawString(320 - (MENU_FONT.getWidth("PRESS SPACE") / 2), 280, "PRESS SPACE", fadeColor);				
					break;
				case CONTINUE_GAME:
					for(int i = 0; i < currentMenu.length; i++)
					{
						Color c = Color.white;
						if(titleSelection == i)
						{							
							c = Color.red;
						}
						switch(i)
						{
							default:
								MENU_FONT.drawString(320 - (MENU_FONT.getWidth(currentMenu[i].toUpperCase()) / 2),  280 + (MENU_FONT.getHeight() * i), currentMenu[i].toUpperCase(), c);						
								break;
						}
					}					
					break;
				case SETTINGS:
					for(int i = 0; i < currentMenu.length; i++)
					{
						if(soundTestclock != null)
						{
							if(soundTestclock.updateClock() && titleSelection == 2)
							{
								Sound.playSound("trevwif0");
							}
						}
						
						Color c = Color.white;
						String menuText;
						if(titleSelection == i)
						{							
							c = Color.red;
						}
						switch(i)
						{
							case 0:
								// Fullscreen
								menuText = currentMenu[i].toUpperCase() + ": " + booleanToPrettyText(Display.isFullscreen()).toUpperCase();
								MENU_FONT.drawString(320 - (MENU_FONT.getWidth(menuText) / 2),  280 + (MENU_FONT.getHeight() * i), menuText, c);
								break;
							case 1:
								// V-Sync
								menuText = currentMenu[i].toUpperCase() + ": " + booleanToPrettyText(TrevorLegacy.vsync).toUpperCase();
								MENU_FONT.drawString(320 - (MENU_FONT.getWidth(menuText) / 2),  280 + (MENU_FONT.getHeight() * i), menuText, c);															
								break;
							case 2:
								// Sound volume
								menuText = currentMenu[i].toUpperCase() + ": ";
								MENU_FONT.drawString(320 - ((MENU_FONT.getWidth(menuText) + 120) / 2),  280 + (MENU_FONT.getHeight() * i), menuText, c);								

								Graphics.drawQuad(320 - ((MENU_FONT.getWidth(menuText) + 120) / 2) + MENU_FONT.getWidth(menuText), 282 + (MENU_FONT.getHeight() * i) + 3, (Sound.getSoundEffectVolume() / 1.0f) * 120, MENU_FONT.getHeight() - 6, c.getRed() / 255, c.getGreen() / 255, c.getBlue() / 255, 1);
								Graphics.drawRect(320 - ((MENU_FONT.getWidth(menuText) + 120) / 2) + MENU_FONT.getWidth(menuText), 282 + (MENU_FONT.getHeight() * i) + 3, 120, MENU_FONT.getHeight() - 6, 1, 1, 1, 1, 2);								
								break;
							case 3:
								// Music volume
								menuText = currentMenu[i].toUpperCase() + ": ";
								MENU_FONT.drawString(320 - ((MENU_FONT.getWidth(menuText) + 120) / 2),  280 + (MENU_FONT.getHeight() * i), menuText, c);

								Graphics.drawQuad(320 - ((MENU_FONT.getWidth(menuText) + 120) / 2) + MENU_FONT.getWidth(menuText), 282 + (MENU_FONT.getHeight() * i) + 3, (Sound.getMusicVolume() / 1.0f) * 120, MENU_FONT.getHeight() - 6, c.getRed() / 255,  c.getGreen() / 255,  c.getBlue() / 255, 1);								
								Graphics.drawRect(320 - ((MENU_FONT.getWidth(menuText) + 120) / 2) + MENU_FONT.getWidth(menuText), 282 + (MENU_FONT.getHeight() * i) + 3, 120, MENU_FONT.getHeight() - 6, 1, 1, 1, 1, 2);
								break;
							default:
								MENU_FONT.drawString(320 - (MENU_FONT.getWidth(currentMenu[i].toUpperCase()) / 2),  280 + (MENU_FONT.getHeight() * i), currentMenu[i].toUpperCase(), c);
								break;
						}												
					}
					break;	
				case ABOUT:
					MENU_FONT.drawString(120, 280, "Trevor Legacy");
					MENU_FONT.drawString(120, 300, "(c) S&F Software, 2014");
					MENU_FONT.drawString(120, 340, "Created by Steve Thompson");
					MENU_FONT.drawString(120, 360, "Music by Andy Woodward");
					MENU_FONT.drawString(120, 400, "http://sfsoftware.sevensoupcans.com");
					break;
				default:
					
					for(int i = 0; i < currentMenu.length; i++)
					{
						Color c = Color.white;
						if(titleSelection == i)
						{							
							c = Color.red;
						}
						
						MENU_FONT.drawString(320 - (MENU_FONT.getWidth(currentMenu[i].toUpperCase()) / 2),  280 + (MENU_FONT.getHeight() * i), currentMenu[i].toUpperCase(), c);						
					}
					break;
			}
		}

		Graphics.drawSprite(64, TitleY - (titleHeight / 2), "title", 512, titleHeight, 0, 0, 512, 128, 1, 1, 1, (float) titleHeight / 128);	      					
	}	
	
}
